package com.boge.entity;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import com.boge.parse.Amf3Stream;

import flex.messaging.io.amf.Amf3Output;

public class GameOutput {

	private ByteArrayOutputStream outputStream;
	private Amf3Output amf3Output;

	public GameOutput() {
		this.outputStream = new ByteArrayOutputStream();
		amf3Output = Amf3Stream.newAmf3Output(outputStream);
	}
	
	public GameOutput(ByteArrayOutputStream outputStream) {
		this.outputStream = outputStream;
		amf3Output = Amf3Stream.newAmf3Output(outputStream);
	}
	
	/**
	 * 创建一个新分配的 byte 数组。其大小是此输出流的当前大小，并且缓冲区的有效内容已复制到该数组中。 
	 * @return 以 byte 数组的形式返回此输出流的当前内容。
	 */
	public byte[] toByteArray() {
		return outputStream.toByteArray();
	}
	
	public GameOutput reset() {
		amf3Output.reset();
		outputStream.reset();
		return this;
	}
	
	/**
	 * 写入list数据 　只能存 int long utf  boolean
	 * @param list
	 * @return
	 * @throws IOException
	 */
	public GameOutput writeList(List<?> list) throws IOException {
		int len = list.size();
		this.writeInt(len);
		Object obj;
		for (int i = 0; i < len; i++) {
			obj = list.get(i);
			if (obj instanceof Long) {
				this.writeInt(1);
				this.writeLong((Long) obj);
			} else if (obj instanceof Integer) {
				this.writeInt(2);
				this.writeInt((Integer) obj);
			} else if (obj instanceof Boolean) {
				this.writeInt(3);
				this.writeBoolean((Boolean) obj);
			} else {
				this.writeInt(0);
				this.writeUTF((String) obj);
			}
		}
		return this;
	}
	
	public GameOutput writeUTF(String s) throws IOException {
		if (s == null) {
			s = "";
		}
		amf3Output.writeUTF(s);
		return this;
	}
	
	public GameOutput writeInt(int v) throws IOException {
		amf3Output.writeInt(v);
		return this;
	}
	
	public GameOutput writeShort(int v) throws IOException {
		amf3Output.writeShort(v);
		return this;
	}
	
	public GameOutput writeFloat(float v) throws IOException {
		amf3Output.writeFloat(v);
		return this;
	}
	
	public GameOutput writeDouble(double v) throws IOException {
		amf3Output.writeDouble(v);
		return this;
	}
	
	public GameOutput writeLong(long v) throws IOException {
		amf3Output.writeLong(v);
		return this;
	}
	
	public GameOutput writeByte(int v) throws IOException {
		amf3Output.writeByte(v);
		return this;
	}
	
	public GameOutput writeObject(Object obj) throws IOException {
		obj = obj==null?"":obj;
		amf3Output.writeObject(obj);
		return this;
	}
	
	public GameOutput writeBoolean(boolean v) throws IOException {
		amf3Output.writeBoolean(v);
		return this;
	}
	
	public GameOutput write(byte[] bytes, int offset, int length) throws IOException {
		amf3Output.write(bytes, offset, length);
		return this;
	}
	
	public void close() throws IOException {
		if(this.amf3Output!=null) this.amf3Output.close();
		if(this.outputStream!=null) this.outputStream.close();
		this.amf3Output = null;
		this.outputStream = null;
	}
	
	/**
	 * 自动判断类型写入
	 * @param arge 自动判断类型包括(Boolean、Integer、Long、byte[]、其余全是String)
	 * @return
	 * @throws IOException
	 */
	public GameOutput write(Object ...arge) throws IOException {
		Object obj;
		byte[] b;
		for (int i = 0; i < arge.length; i++) {
			obj = arge[i];
			if (obj instanceof Boolean) {
				writeBoolean((Boolean) obj);
			} else if (obj instanceof Integer) {
				writeInt((Integer) obj);
			} else if (obj instanceof Long) {
				writeLong((Long) obj);
			} else if (obj instanceof byte[]) {
				b = (byte[]) obj;
				write(b, 0, b.length);
			} else if (obj instanceof List<?>) {
				writeList((List<?>) obj);
			} else {
				writeUTF(obj==null?"":obj.toString());
			}
		}
		return this;
	}

	public ByteArrayOutputStream getOutputStream() {
		return outputStream;
	}

	public Amf3Output getAmf3Output() {
		return amf3Output;
	}
	
	
}
